$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/depothead/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'Id', width: 50, key: true },
			{ label: '类型(出库/入库)', name: 'type', index: 'Type', width: 80 }, 			
			{ label: '出入库分类', name: 'subtype', index: 'SubType', width: 80 }, 			
			{ label: '项目Id', name: 'projectid', index: 'ProjectId', width: 80 }, 			
			{ label: '初始票据号', name: 'defaultnumber', index: 'DefaultNumber', width: 80 }, 			
			{ label: '票据号', name: 'number', index: 'Number', width: 80 }, 			
			{ label: '操作员名字', name: 'operpersonname', index: 'OperPersonName', width: 80 }, 			
			{ label: '创建时间', name: 'createtime', index: 'CreateTime', width: 80 }, 			
			{ label: '出入库时间', name: 'opertime', index: 'OperTime', width: 80 }, 			
			{ label: '供应商Id', name: 'organid', index: 'OrganId', width: 80 }, 			
			{ label: '采购/领料-经手人Id', name: 'handspersonid', index: 'HandsPersonId', width: 80 }, 			
			{ label: '账户Id', name: 'accountid', index: 'AccountId', width: 80 }, 			
			{ label: '变动金额(收款/付款)', name: 'changeamount', index: 'ChangeAmount', width: 80 }, 			
			{ label: '调拨时，对方项目Id', name: 'allocationprojectid', index: 'AllocationProjectId', width: 80 }, 			
			{ label: '合计金额', name: 'totalprice', index: 'TotalPrice', width: 80 }, 			
			{ label: '付款类型(现金、记账等)', name: 'paytype', index: 'PayType', width: 80 }, 			
			{ label: '备注', name: 'remark', index: 'Remark', width: 80 }, 			
			{ label: '业务员（可以多个）', name: 'salesman', index: 'Salesman', width: 80 }, 			
			{ label: '多账户ID列表', name: 'accountidlist', index: 'AccountIdList', width: 80 }, 			
			{ label: '多账户金额列表', name: 'accountmoneylist', index: 'AccountMoneyList', width: 80 }, 			
			{ label: '优惠率', name: 'discount', index: 'Discount', width: 80 }, 			
			{ label: '优惠金额', name: 'discountmoney', index: 'DiscountMoney', width: 80 }, 			
			{ label: '优惠后金额', name: 'discountlastmoney', index: 'DiscountLastMoney', width: 80 }, 			
			{ label: '销售或采购费用合计', name: 'othermoney', index: 'OtherMoney', width: 80 }, 			
			{ label: '销售或采购费用涉及项目Id数组（包括快递、招待等）', name: 'othermoneylist', index: 'OtherMoneyList', width: 80 }, 			
			{ label: '销售或采购费用涉及项目（包括快递、招待等）', name: 'othermoneyitem', index: 'OtherMoneyItem', width: 80 }, 			
			{ label: '结算天数', name: 'accountday', index: 'AccountDay', width: 80 }, 			
			{ label: '单据状态(未审核、已审核)', name: 'status', index: 'Status', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		depothead: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.depothead = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.depothead.id == null ? "sys/depothead/save" : "sys/depothead/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.depothead),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/depothead/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/depothead/info/"+id, function(r){
                vm.depothead = r.depothead;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});